package com.priusoft.api.controller;

import com.priusoft.api.domain.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/redis")
public class RedisTestController {

    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("/test1")
    @ResponseBody
    public String test1() {
        redisTemplate.opsForHash().put("key3","f1","hello1");

        List list1 = new ArrayList();
        list1.add("aa");
        list1.add("bb");

        List list2 = new ArrayList();
        list2.add("cc");
        list2.add("dd");

        Pagination pagination = new Pagination();
        pagination.setPageIndex(10);
        pagination.setPageSize(20);


        Map map = new HashMap();
        map.put("a1",list1);
        map.put("a2",list2);
        map.put("a3",pagination);
        redisTemplate.opsForHash().putAll("key4",map);
        return "write key";
    }

    @RequestMapping("/test2")
    @ResponseBody
    public String test2() {
        return redisTemplate.opsForHash().get("key3","f1").toString();
    }

    @RequestMapping("/test3")
    @ResponseBody
    public Pagination test3() {
        return (Pagination)redisTemplate.opsForHash().get("key4","a3");
    }

}
